<?php
/**
 * User: Mark Johnson
 * Date: Oct 22, 2010
 * Time: 5:22:07 PM
 * 
 * This file is primarily for development purposes.
 * 
 * It displays in tables the entire contents of the BugZapp database.
 * 
 * This file also verifies that the development data was added correctly to the database
 * from the install script. This only happens if /data?op=verifydev is used as the URL
 * 
 */
 
$main = 'data';

/**
 * data()
 * 
 * @param mixed $vars
 * @return void
 */
function data(&$vars)
{
	system_add_styles('assets/css/style.css');
	system_add_js('assets/js/jquery.js');
	
	$breadcrumbs = array(
		'Home' => array(
			'#title' => 'Home',
			'#anchor' => 'Home',
			'#attributes' => array(
				'class' => 'breadcrumb'
			),
			'#href' => base_path()
		)
	);
	system_breadcrumbs($breadcrumbs);
	
	system_page_title('Data Tables');
}

/**
 * data_prerender_page()
 * 
 * @param mixed $vars
 * @return void
 */
function data_prerender_page(&$vars)
{
	$output = '';
	
	$verify = FALSE;
	if (isset($_GET['op']) && $_GET['op'] == 'verifydev')
	{
		$verify = TRUE;
	}
	
	$retVal = TRUE;
	
	/***** Developers *****/
	
	$result = db_query('SELECT * FROM developer WHERE did > 0');
	
	if ($verify && mysql_num_rows($result) != 30)
	{
		$retVal = FALSE;
		system_set_message('Development data for the table <code>developer</code> did not fully load.');
	}
	
	$output .= '<table border="1" cellpadding="10" cellspacing="0" width="100%" style="margin: 20px 0;"><thead><tr><th>Developer ID</th><th>Name</th><th>Email</th></tr></thead><tbody>';
	while ($dev = db_fetch_array($result))
	{
		$output .= '<tr><td>' . $dev['did'] . '</td><td>' . $dev['name'] . '</td><td>' . $dev['email'] . '</td></tr>';
	}
	$output .= '</tbody></table>';
	
	/***** Features *****/
	
	$result = db_query('SELECT * FROM feature WHERE fid > 0');
	if ($verify && mysql_num_rows($result) != 100)
	{
		$retVal = FALSE;
		system_set_message('Development data for the table <code>feature</code> did not fully load.');
	}
	
	$output .= '<table border="1" cellpadding="10" cellspacing="0" width="100%" style="margin: 20px 0;"><thead><tr><th>Feature ID</th><th>Description</th><th>Details</th></tr></thead><tbody>';
	while ($feature = db_fetch_array($result))
	{
		$output .= '<tr><td>' . $feature['fid'] . '</td><td>' . $feature['description'] . '</td><td>' . $feature['details'] . '</td></tr>';
	}
	$output .= '</tbody></table>';
	
	/***** Milestones *****/
	
	$result = db_query('SELECT * FROM milestone WHERE mid > 0');
	if ($verify && mysql_num_rows($result) != 10)
	{
		$retVal = FALSE;
		system_set_message('Development data for the table <code>milestone</code> did not fully load.');
	}
	
	$output .= '<table border="1" cellpadding="10" cellspacing="0" width="100%" style="margin: 20px 0;"><thead><tr><th>Milestone ID</th><th>Name</th><th>Date</th></tr></thead><tbody>';
	while ($mile = db_fetch_array($result))
	{
		$output .= '<tr><td>' . $mile['mid'] . '</td><td>' . $mile['name'] . '</td><td>' . date('r', $mile['date']) . '</td></tr>';
	}
	$output .= '</tbody></table>';
	
	/***** Tasks *****/
	
	$result = db_query('SELECT * FROM task WHERE tid > 0');
	if ($verify && mysql_num_rows($result) != 250)
	{
		$retVal = FALSE;
		system_set_message('Development data for the table <code>task</code> did not fully load.');
	}
	
	$status = array('P' => 'Pending', 'I' => 'In Progres', 'C' => 'Completed');
	$output .= '<table border="1" cellpadding="10" cellspacing="0" width="100%" style="margin: 20px 0;"><thead><tr><th>Task ID</th><th>Description</th><th>Details</th><th>Developer ID</th><th>Feature ID</th><th>Milestone ID</th><th>EET</th><th>Status</th></tr></thead><tbody>';
	while ($task = db_fetch_array($result))
	{
		$output .= '<tr><td>' . $task['tid'] . '</td><td>' . $task['description'] . '</td><td>' . $task['details'] . '</td><td>' . $task['did'] . '</td><td>' . $task['fid'] . '</td><td>' . $task['mid'] . '</td><td>' . (int)($task['eet'] / 86400) . ' days</td><td>' . $status[$task['status']] . '</td></tr>';
	}
	$output .= '</tbody></table>';
	
	if ($verify)
	{
		$output = ($retVal ? '<h2>Congratulations! The development data fully loaded into the database</h2>' : '<h2 class="error">The development data failed to fully load into the database.</h2>') . $output;
	}
	
	$vars['content'] = $output;
	
	$vars['tabs'] = NULL;
	$vars['messages'] = render('system_messages', system_set_message());
}
